import requests
import re
import time
import parsel


def aff():
    cookie_Token_ = login_1()
    url = 'http://aafgzx.nextsls.com/wos/shipment/index?kw=GZBL2202029916&waybill_number=&service=&daterange=&created_daterange=&country=&name=&store_id='
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': 'keep-alive',
        'Cookie': '{}'.format(cookie_Token_[1]),
        'Host': 'aafgzx.nextsls.com',
        'Referer': 'http://aafgzx.nextsls.com/wos/shipment/index?status=picked',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',

    }

    response = requests.get(url, headers=headers).text
    # print(response)
    response_parsel = parsel.Selector(response)
    response_parsel_country = response_parsel.css('#w0 > table > tbody > tr > td:nth-child(5)::text').extract_first()
    # print(response_parsel_country)
    response_tbody = re.findall('<tbody>(.*?)</tbody>', response, re.S)[0]
    response_data_key = re.findall('data-key="(.*?)"', response_tbody)[0]
    # print(response_data_key)
    return [cookie_Token_[0], cookie_Token_[1], response_data_key, response_parsel_country]


def aff_1():
    # print('opooo')
    times = int(time.time() * 1000)
    id_cookie_token = aff()
    url = 'http://aafgzx.nextsls.com/wos/shipment/show_traces?id={}&lmodal=1&_={}'.format(
        id_cookie_token[2], times)
    # print(url)
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': 'keep-alive',
        'Cookie': '{}'.format(id_cookie_token[1]),
        'Host': 'aafgzx.nextsls.com',
        'Referer': 'http://aafgzx.nextsls.com/wos/shipment/index?status=delivered',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
        'X-CSRF-Token': '{}'.format(id_cookie_token[0]),
        'X-Requested-With': 'XMLHttpRequest',
    }
    # print(headers)
    param = {
        'id': '{}'.format(id_cookie_token[2]),
        'lmodal': '1',
        '_': '{}'.format(times)

    }
    response = requests.get(url, headers=headers, params=param).text
    # print(response)
    response_parsel = parsel.Selector(response)
    response_parsel_ = response_parsel.css('#one-column > div')
    # print(response_parsel_)
    for response_parsel_div in response_parsel_:
        print(id_cookie_token[3])
        response_parsel_div_ = response_parsel_div.css('div > div.message-head.clearfix::text').get().strip()
        print(response_parsel_div_)
        response_parsel_div_span = response_parsel_div.css('div > div.qa-message-content span::text').get().strip()
        print(response_parsel_div_span)


def login_():
    url = 'http://aafgzx.nextsls.com/wos/login/'
    headers = {

        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': 'grid-view-pagesize=10',
        'Host': 'aafgzx.nextsls.com',
        'Referer': 'http://aafgzx.nextsls.com/wos/login/',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',

    }
    response = requests.get(url, headers=headers)
    # print(response.headers['Set-Cookie'])
    # print(re.findall('<meta name="csrf-token" content="(.*)">', response.text))
    return [response.headers['Set-Cookie'], re.findall('<meta name="csrf-token" content="(.*)">', response.text)[0]]


def login_1():
    cookie_Token = login_()
    url = 'http://aafgzx.nextsls.com/wos/login'
    headers = {

        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': '{}'.format(cookie_Token[0]),
        'Host': 'aafgzx.nextsls.com',
        'Origin': 'http://aafgzx.nextsls.com',
        'Referer': 'http://aafgzx.nextsls.com/wos/login/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
        'X-CSRF-Token': '{}'.format(cookie_Token[1]),
        'X-Requested-With': 'XMLHttpRequest',

    }

    data = {
        'username': '广州盛业',
        'password': '123456',
        'inajax': '1'
    }
    response = requests.post(url, headers=headers, data=data).headers
    cookie = '_csrf-parcelos-wos' + re.findall('_csrf-parcelos-wos(.*?);', cookie_Token[0])[0] + ';' + 'parcelos_wos=' + \
             re.findall('parcelos_wos=(.*?);', response['Set-Cookie'])[0]
    # print(cookie)
    return [cookie_Token[1], cookie]


if __name__ == '__main__':
    aff_1()
